import { Component, OnInit } from '@angular/core';
import { Subject, timer } from 'rxjs';
import { switchMap } from 'rxjs/operators';
import { Router } from '@angular/router';

@Component({
    selector: 'app-buffeting',
    templateUrl: './buffeting.component.html',
    styleUrls: ['./buffeting.component.less'],
})
export class BuffetingComponent implements OnInit {
    navs = [
        { title: '排序表格', link: '/table/sortTable' },
        { title: '表格批量操作', link: '/table/multipleChoice' },
        { title: 'antd弹窗', link: '/antd/modal' },
        { title: 'switchMap使用', link: '/rxjsuse/switchMap' },
        { title: 'forkJoin使用', link: '/rxjsuse/forkJoin' },
    ];
    private clickSubject = new Subject<string>();
    private delayMs = 3000; // 设置延迟时间为3000毫秒

    ngOnInit(): void {}

    constructor(private router: Router) {
        this.clickSubject
            .pipe(
                switchMap((link) =>
                    timer(this.delayMs).pipe(
                        switchMap(() => {
                            // 在这里执行实际的路由跳转
                            this.router.navigate([link]);
                            return [];
                        })
                    )
                )
            )
            .subscribe();
    }

    onNavClick(link: string) {
        event.preventDefault(); // 阻止默认的点击行为
        this.clickSubject.next(link);
    }
}
